<?php
namespace app\common\library\zbx;

class Lib{
    
    public static $host = "http://zbx.92mj.vip/api_jsonrpc.php";
    
    public static function zbx_user_login()
    {
        $params = [
            'user'     => 'ns001',
            'password' => 'kkk123321@@@'
        ];
        $resp_str = self::curl_post('user.login', $params);
        $resp = json_decode($resp_str);
        if(isset($resp->error))
            return json_encode($resp->error);
        else
	    return $resp->result;
    }
    
    public static function get_session()
    {
        $session = '';
        $content = file_get_contents("zabbix_session.json");
        $data = null;
        
        if($content == '')
        {
            $session = self::zbx_user_login();
            if($session == '')
                return '';
            
            $data['session'] = $session;
            $data['expire']  = time() + 7200;
            $fp = fopen('zabbix_session.json', 'w');
            fwrite($fp, json_encode($data));
            fclose($fp);
            return $session;
        }else{
            $data = json_decode($content);
            if(isset($data->expire) && $data->expire > time())
            {
                return $data->session;
            }else{
                $session = self::zbx_user_login();
                if($session == '')
                    return '';
                
                $new['session'] = $session;
                $new['expire']  = time() + 7200;
                $fp = fopen('zabbix_session.json', 'w');
                fwrite($fp, json_encode($data));
                fclose($fp);
                return $session;
            }
        }
        
    }
    
    public static function RPC($method, $params, $auth = null, $isorigin = false)
    {
        $resp_str = self::curl_post($method, $params, self::get_session());
        
        $resp = json_decode($resp_str);
        
        if(isset($resp->result))
            $data = $resp->result;
        else if(isset($rep->error))
       	    $data = $resp->error;
 
        if(!$isorigin)
            return json_encode([
                'code'  => 'SUCCESS',
                'data'  => $data
            ]);
        else
            //return $resp->result;
	    //return self::get_session();
	    //return isset($resp->result)?$resp->result:isset($resp->error)?$resp->error:$resp;
	    //return $resp->result;
            return $resp;
    }
    
    
    
    
    public static function curl_post($method, $params, $auth = null)
    {
        $curl = curl_init();
        
        curl_setopt_array($curl, array(
            CURLOPT_URL => self::$host,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode(array(
                "jsonrpc" => "2.0",
                "method"  => $method,
                "params"  => $params,
                "id"      => 1,
                "auth"    => $auth
            )),
            CURLOPT_HTTPHEADER => array(
                "Content-Type: application/json"
            ),
        ));
        
        $response = curl_exec($curl);
        
        curl_close($curl);
        
        return $response;
    }
}
